home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / tools / kernelgen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-15  |  3.3 KB  |  128 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * kernel_gen -- Copyright (C) 2000 Sven Neumann <sven@gimp.org> 
  5.  *    Simple hack to create subsampling kernels for the brushes
  6.  *    as used in app/paint_core.c.
  7.  *    If you want to play with it, change some of the #defines at the
  8.  *    top and copy the output to apps/paint_core_kernels.h.
  9.  * 
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  */
  24. #include <math.h>
  25. #include <stdio.h>
  26.  
  27.  
  28. #define STEPS          64
  29. #define KERNEL_WIDTH    3     /*  changing these makes no sense  */
  30. #define KERNEL_HEIGHT   3     /*  changing these makes no sense  */
  31. #define SUBSAMPLE       4
  32. #define THRESHOLD       0.25  /*  try to change this one         */
  33.  
  34. #define SQR(x) ((x) * (x))
  35.  
  36. static void
  37. create_kernel (double x,
  38.            double y)
  39. {
  40.   double value[KERNEL_WIDTH][KERNEL_HEIGHT];
  41.   double dist_x;
  42.   double dist_y;
  43.   double sum;
  44.   double w;
  45.   int i, j;
  46.  
  47.   memset (value, 0, KERNEL_WIDTH * KERNEL_HEIGHT * sizeof (double));
  48.   sum = 0.0;
  49.  
  50.   x += 1.0;
  51.   y += 1.0;
  52.  
  53.   for (j = 0; j < STEPS * KERNEL_HEIGHT; j++)
  54.     {
  55.       dist_y = y - (((double)j + 0.5) / (double)STEPS);
  56.  
  57.       for (i = 0; i < STEPS * KERNEL_WIDTH; i++)
  58.     {
  59.       dist_x = x - (((double)i + 0.5) / (double)STEPS);
  60.       
  61.       /*  I've tried to use a gauss function here instead of a
  62.           threshold, but the result was not that impressive.    */
  63.       w = (SQR (dist_x) + SQR (dist_y)) < THRESHOLD ? 1.0 : 0.0;
  64.  
  65.       value[i / STEPS][j / STEPS] += w;
  66.       sum += w;
  67.     }
  68.     }
  69.  
  70.   for (j = 0; j < KERNEL_HEIGHT; j++)
  71.     {
  72.       for (i = 0; i < KERNEL_WIDTH; i++)
  73.     {
  74.       w = 256.0 * (value[i][j] / sum);
  75.       printf (" %3d,", (int)w);
  76.     }
  77.     }    
  78. }
  79.  
  80. int 
  81. main (int    argc,
  82.       char **argv)
  83. {
  84.   int    i, j;
  85.   double x, y;
  86.  
  87.   printf ("/* paint_core_kernels.h\n"
  88.       " *\n"
  89.       " *   This file was generated using kernelgen as found in the tools dir.\n");
  90.   printf (" *   (threshold = %g)\n", THRESHOLD);
  91.   printf (" */\n\n");
  92.   printf ("#define KERNEL_WIDTH   %d\n", KERNEL_WIDTH);
  93.   printf ("#define KERNEL_HEIGHT  %d\n", KERNEL_HEIGHT);
  94.   printf ("#define SUBSAMPLE      %d\n", SUBSAMPLE);
  95.   printf ("\n\n");
  96.   printf ("/*  Brush pixel subsampling kernels  */\n");
  97.   printf ("static const int subsample[%d][%d][%d] = {\n",
  98.       SUBSAMPLE + 1, SUBSAMPLE + 1, KERNEL_WIDTH * KERNEL_HEIGHT);
  99.   
  100.   for (j = 0; j <= SUBSAMPLE; j++)
  101.     {
  102.       y = (double)j / (double)(SUBSAMPLE);
  103.  
  104.       printf ("  {\n");  
  105.  
  106.       for (i = 0; i <= SUBSAMPLE; i++)
  107.     {
  108.       x = (double)i / (double)(SUBSAMPLE);
  109.  
  110.       printf ("    {");
  111.       create_kernel (x, y);
  112.       printf (" }%c\n", i < SUBSAMPLE ? ',' : ' ');
  113.     }
  114.  
  115.       printf ("  }%c\n", j < SUBSAMPLE ? ',' : ' ');
  116.     }
  117.  
  118.   printf ("};\n");
  119.  
  120.   exit (0);
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.